package file;

import java.io.*;
import java.util.Scanner;

//文件的复制
public class Demo9 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入源文件路径：");
        String srcPath = sc.next();
        File srcFile = new File(srcPath);
        if(!srcFile.isFile()){
            System.out.println("当前路径不是一个文件！");
            return;
        }
        System.out.println("请输入目标文件路径：");
        String descPath = sc.next();

        File descFile = new File(descPath);
        //目标文件路径不一定需要存在文件，但一定需要存在目录
        if(!descFile.getParentFile().isDirectory()){
            System.out.println("目标文件所在目录不存在");
            return;
        }

        //读和写操作
        try(InputStream inputStream = new FileInputStream(srcFile);
        OutputStream outputStream = new FileOutputStream(descFile)) {
            while(true){
                byte[] bytes = new byte[1024];
                int n = inputStream.read(bytes);
                if(n == -1){
                    break;
                }
                outputStream.write(bytes,0,n);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
